C Source Formatter:

The CTools™ C source formatter works beautifully.  It only works on files suffixed in “.c” though.  

The C source formatter will operate on a “Command 1” key combination on the open file, whereupon it does it and replaces the ext in the file with the formatted text.  If you want to keep this, then close and save, or vice versa.  Otherwise you MUST select “Save As” from the file menu if you do not want your original source altered.

I have never had an error in its output as long as the input was compilable code and not too big.  Sometimes it will balk on a BIG ile, or one with some unusual weirdnesses.  But that’s very rare.  If it does do this, it will merely crash without eating your file (hopefully).  What I do, when I KNOW I have too big a file to format, is split it up.  Open the CTools™ “WorkSheet.c” file, select half the file to be processed file, copy it, paste it into the worksheet, format it, select all, cut, save the worksheet, switch back to the intended file and paste and save.  Then select the other half.  You can do this between your editor and CTools™ or all in CTools™.

The formatter doubles as a “C check” function, in that if there is a missing brace or missing semicolon or missing paren, the Source Formatter output will have odd indents immediately afterward.  You can correct these glitches right in the CTools™ window.  Scroll down until the code looks oddly indented, find the omission, add it, and run the formatter again.

If you have a missing “case” keyword before a ‘:’ then CTools™ will not indent that line, so those are easy to find.

You can also paste a routine into a new CTools™ document being used as a “worksheet” and format it, then cut or copy it out.  The file must be “saved” with the “.c” suffix in order for it to work.  But if you want to pump code and not deal with indents until you are done, then just have CTools™ running in MultiFinder and, when you’re done typing, select all and copy, paste into your CTools™ “worksheet.c”, hit Command 1, select all and cut again, save the worksheet.c file (save empty) and then paste your source back into your code file.

One tip on big files, suppose you have a huge routine and CTools™ doesn’t have enough “poop” to process it?  Simply:

( 1) Move the insertion point to the top of the file in your code editor.
( 2) Drag the scroll thimb halfway down.
( 3) Hold SHIFT and click near the left margin.
( 4) Command V.
( 5) Switch to the CTools™ Worksheet.c window.
( 6) Command 1.
( 7) Command A.
( 8) Command X.
( 9)Commad S.
(10) Switch back to your editor.
(11) Command V.  

Now, the tricky part...

You’ll want the remaining, unformatted source from the file to have the same indent as the part it goes under IF you are mid-routine.  If you format the remainder the way it is, missing begin braces will result in mismatched indents.  So, do this:

( 1) Put the insertion point just before the first character of the last 
        line of the code you just formatted.
( 2) Hit the left arrow and count how many times you hit it to get the
        insertion point to the left margin.
( 3) Hit the down arrow to get below that code.
( 4) Drag the scroll thumb to the bottom of the file.
( 5) Hold Shift and click past the end of all the code.
( 6) Command C.
( 7) Switch back to the CTools™ Worksheet.c window.
( 8) Type the same number of begin braces as the number of left arrow hits 
        you counted earlier.
( 9) Command V.
(10) Command 1.
(11) Command X.
(12) Command S.
(13) Switch back to your editor.
(14) Command V.

I will be working in more user configurability but, until then, YOU have to do a little configuration on your own if you do not want your source to appear as CTools™ puts it out - but not much.

If a brace is on the same line as an 'if' statement it will stay there.  If you want it on its own, new line, pre-edit by using a Find/Replace function in a text editor.  Find " " (space leftBrace) and Replace All with "<cr>" (return leftBrace) before running the formatter.  A Find/Replace function will be added to CTools™, soon, so you can do this “in-house”, but it will probably just be a part of the preferences.

If you want to see how good this formatter is, preprocess a small source file, replace all RETURNs with a space, save the result and run the formatter on it!  

If you intend to clean up CTools™ output from the Pascal to C function, you MUST edit it so that it has ALL matching braces, parens and semicolons first.  The technique for doing this is detailed in the Pascal to C section.